package capapplet;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.URL;
import java.util.Vector;
import java.util.StringTokenizer;
import cap.LecteurCap;
import affcom.*;

public class CapApplet extends Applet
{
    LecteurCap lecteurCap;

    Label label1 = new Label();
    Button buttonLecture = new Button();
    Button buttonEcriture = new Button();
    Choice choice1 = new Choice();
    Label label4 = new Label();
    TThumb editCode = new TThumb(4);
    String strAllAcces;
    Vector vecAllAcces;
    String strNumeroSelectedBorne;
    TPanel panel1 = new TPanel();
    TextField textField1 = new TextField();
    Label label2 = new Label();
    TPanel panel2 = new TPanel();
    GridBagLayout gridBagLayout1 = new GridBagLayout();

    /**Obtenir une valeur de paramètre*/
    public String getParameter(String key, String def) {
	return getParameter(key) != null ? getParameter(key) : def;
    }

    /**Construire l'applet*/
    public CapApplet()
    {
    }

    /**Initialiser l'applet*/
    public void init()
    {
	URL myURLServeur =  getDocumentBase();  // c'est la meme servlet qui gere les requetes (POST)
	lecteurCap = new LecteurCap(myURLServeur);
	try
	{
	    jbInit();
	}
	catch(Exception e) {
	    e.printStackTrace();
	}
	try
	{
	    strAllAcces = this.getParameter("allacces", "");
	}
	catch(Exception e) {e.printStackTrace();}
	//System.out.println("strAllAcces: " + strAllAcces);
	vecAllAcces = parseUrlAcces(strAllAcces);
	int l = vecAllAcces.size();
	for( int i = 0; i<l; i++)
	{
	    choice1.add(getNameAcces(i));
	}
    }

    /**Initialiser le composant*/
    private void jbInit() throws Exception
    {
	buttonLecture.setBackground(new Color(150, 235, 150));
	buttonLecture.setLabel("Lire");
	buttonLecture.setBounds(new Rectangle(121, 10, 103, 27));
	buttonLecture.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		buttonLecture_actionPerformed(e);
	    }
	});

	buttonEcriture.setBackground(new Color(235, 130, 130));
	buttonEcriture.setLabel("Programmer");
	buttonEcriture.setBounds(new Rectangle(120, 46, 104, 27));
	buttonEcriture.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		buttonEcriture_actionPerformed(e);
	    }
	});
	choice1.setBounds(new Rectangle(130, 157, 113, 19));
	choice1.addItemListener(new java.awt.event.ItemListener() {
	    public void itemStateChanged(ItemEvent e) {
		choice1_itemStateChanged(e);
	    }
	});

	label1.setText("Programmation code carte à puce");
	label1.setBounds(new Rectangle(29, 5, 213, 21));


	label4.setText("Choix Borne");
	label4.setBounds(new Rectangle(12, 157, 81, 20));

	editCode.setEditTint(Color.orange);
	editCode.setEdit(true);


	this.setBackground(Color.lightGray);
	this.setEnabled(true);
	this.setLayout(null);

	panel1.setLayout(null);

	panel1.setBackground(new Color(142, 142, 142));
	panel1.setBounds(new Rectangle(10, 31, 234, 85));
	textField1.setEditable(false);
	textField1.setText("Pas de borne sélectionnée");
	textField1.setBounds(new Rectangle(54, 126, 190, 23));
	label2.setText("Info");
	label2.setBounds(new Rectangle(10, 127, 39, 22));
	panel2.setBackground(new Color(112, 112, 112));
	panel2.setBounds(new Rectangle(23, 8, 78, 69));
	panel2.setLayout(gridBagLayout1);
	panel1.add(buttonLecture, null);
	panel1.add(buttonEcriture, null);
	panel1.add(panel2, null);
	panel2.add(editCode,  new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
		,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(3, 8, 2, 6), -8, -1));
	this.add(label2, null);
	this.add(label4, null);
	this.add(label1, null);
	this.add(textField1, null);
	this.add(choice1, null);
	this.add(panel1, null);
    }

    /**Démarrer l'applet*/
    public void start() {
    }
    /**Arrêter l'applet*/
    public void stop() {
    }
    /**Détruire l'applet*/
    public void destroy() {
    }
    /**Obtenir les informations d'applet*/
    public String getAppletInfo() {
	return "Information applet";
    }
    /**Obtenir les informations de paramètre*/
    public String[][] getParameterInfo() {
	return null;
    }

    void buttonLecture_actionPerformed(ActionEvent e)
    {
	if (strNumeroSelectedBorne != null)
	{
	    textField1.setText("lecture en cours...");
	    String s = lecteurCap.readCap(strNumeroSelectedBorne);

	    if (s.length() == 4)
	    {
		char firstChar = s.charAt(0);
		if (firstChar == 'A')
		{
		    textField1.setText("carte absente");
		}
		else if (firstChar == 'B')
		{
		    textField1.setText("accès carte impossible");
		}
		else
		{
		    editCode.setText(s);
		    textField1.setText("lecture carte OK");
		}
	    }
	    else textField1.setText("acces borne impossible");
	}
	else textField1.setText("numero borne invalide");

    }

    void buttonEcriture_actionPerformed(ActionEvent e)
    {
	if (strNumeroSelectedBorne != null)
	{
	    textField1.setText("programmation en cours...");
	    String code = editCode.getText();
	    //System.out.println("code: " + code);
	    String s = lecteurCap.writeCap(strNumeroSelectedBorne, code);
	    //System.out.println("s: " + s);
	    if (s.length() == 4)
	    {
		char firstChar = s.charAt(0);
		if (firstChar == 'A')
		{
		    textField1.setText("carte absente");
		}
		else if (firstChar == 'B')
		{
		    textField1.setText("accès carte impossible");
		}
		else
		{
		    editCode.setText(s);
		    if (s.equals(code))
		    {

			textField1.setText("programmation carte OK");
		    }
		    else textField1.setText("erreur programmation carte");
		}
	    }
	    else textField1.setText("acces borne impossible");
	}
	else textField1.setText("numero borne invalide");

    }

    void choice1_itemStateChanged(ItemEvent e)
    {
	strNumeroSelectedBorne = getStrNumeroBorne(choice1.getSelectedItem());
	textField1.setText("numero borne: " + strNumeroSelectedBorne);
    }

    //****************************************************************************
    //****************************************************************************

    private Vector parseUrlAcces(String allAcces)
    {
	Vector result = new Vector();
	//System.out.println("acces: " + allAcces);
	StringTokenizer st = new StringTokenizer(allAcces);
	while (st.hasMoreTokens())
	{
	    String s1 = st.nextToken();
	    String name  = s1.substring(0, s1.indexOf('|'));
	    String strNumeroBorne   = s1.substring(s1.indexOf('|') + 1, s1.indexOf('^'));
	    String url   = s1.substring(s1.indexOf('^') + 1);
	    if ((name != null) & (url != null))
	    {
		Vector v = new Vector(3);
		v.addElement(name);
		v.addElement(strNumeroBorne);
		v.addElement(url);
		result.addElement(v);
	    }
	}
	return result;
    }

    private String getNameAcces(int i)
    {
	return (String)((Vector)vecAllAcces.elementAt(i)).elementAt(0);
    }
    private int getNumeroBorne(int i)
    {
	return ((Integer)((Vector)vecAllAcces.elementAt(i)).elementAt(1)).intValue();
    }

    private String getStrNumeroBorne(String name)
    {
	String result = null;
	int l = vecAllAcces.size();
	for( int i = 0; i<l; i++)
	{
	    String s = getNameAcces(i);
	    if (s.equals(name))
	    {
		result = (String)((Vector)vecAllAcces.elementAt(i)).elementAt(1);
		break;
	    }
	}
	return result;
    }

    private String getUrlAcces(String name)
    {
	String result = null;
	int l = vecAllAcces.size();
	for( int i = 0; i<l; i++)
	{
	    if (getNameAcces(i).equals(name))
	    {
	    result = (String)((Vector)vecAllAcces.elementAt(i)).elementAt(2);
	    break;
	}
	}
	return result;
    }

}